/**
 * Created with IntelliJ IDEA.
 * User: "Balasubramanian"
 * Date: 8/17/13
 * Time: 11:44 AM
 * To change this template use File | Settings | File Templates.
 */

package com.twu.biblioteca;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.logging.Logger;


public class MemberController {

    private HashMap<String,Member> memberList;
    private Logger logger;

    private String MEMBER_LIST_FILE;
    private String SEPARATOR=" ";

    MemberController(String sourceFile,Logger logger)
    {
        MEMBER_LIST_FILE=sourceFile;
        this.logger=logger;

        memberList=new HashMap<>();

        readAllMembersFromFile();
    }

    Member getMemberDetails(String memberNumber)
    {
        if(!memberList.containsKey(memberNumber))
            return null;
        return memberList.get(memberNumber);
    }


    boolean validateMemberLogin(String memberNumber,String password)
    {
        if(!memberList.containsKey(memberNumber))
            return false;

        Member member=memberList.get(memberNumber);
        return member.getPassword().equals(password);
    }

    boolean isValidMemberId(String memberId)
    {
        return memberList.containsKey(memberId);
    }

    void printMemberDetailsToConsole(String memberId)
    {
        Member member=getMemberDetails(memberId);
        System.out.println("MemberName : "+member.getMemberName());
        System.out.println("Email id :"+ member.getEmail());
        System.out.println("Phone number : "+member.getPhoneNumber()+"\n");
    }

    private void readAllMembersFromFile()
    {
        ArrayList<String> linesFromFile=UtilityFunctions.readAllLinesFromFile(MEMBER_LIST_FILE,logger);

        for(String input:linesFromFile)
        {
            String[] fields=input.split(SEPARATOR);
            Member member=new Member(fields[0],fields[1],fields[2],fields[3],fields[4]);
            memberList.put(fields[0],member);
        }
    }

}
